class FireTeamHUD extends GameHUD;

var bool bDrawTraces; //Hold exec console function switch to display debug of trace lines & Paths.

var FontRenderInfo TextRenderInfo; //Font for outputed text to viewport

exec function ToggleIsometricDebug()
{
        bDrawTraces = !bDrawTraces;
        if(bDrawTraces)
        {
                `Log("Showing debug line trace for mouse");
        }
        else
        {
                `Log("Disabling debug line trace for mouse");
        }
}

function DrawTraceDebugRays()
{
        local FireTeamPlayerController PC;
        PC = FireTeamPlayerController(PlayerOwner);

        //Draw Trace from the camera to the world using
        Draw3DLine(PC.StartTrace, PC.EndTrace, MakeColor(255,128,128,255));

        //Draw eye ray for collision and determine if a clear running is permitted(no obstacles between pawn && destination)
        Draw3DLine(PC.PawnEyeLocation, PC.MouseHitWorldLocation, MakeColor(0,200,255,255));
}


function vector2D GetMouseCoordinates()
{
	local Vector2D mousePos;
	local UIInteraction UIController;
	local GameUISceneClient GameSceneClient;

	UIController  = PlayerOwner.GetUIController();

	if ( UIController != None)
	{
			GameSceneClient = UIController.SceneClient;
			if ( GameSceneClient != None )
			{
					mousePos.X = GameSceneClient.MousePosition.X;
					mousePos.Y = GameSceneClient.MousePosition.Y;
			}
	}

	return mousePos;
}

/******************************************************************
 *
 * PostRender event
 *
 * Use postRender function to define and call all hud drawing
 * routine.
 *
 *
 ******************************************************************/
event PostRender()
{
        local FireTeamIsoCam PlayerCam;
        local FireTeamPlayerController PC;

        super.PostRender();

        //Get a type casted reference to our custom player controller.
        PC = FireTeamPlayerController(PlayerOwner);

        //Get the mouse coordinates from the GameUISceneClient
        PC.PlayerMouse = GetMouseCoordinates();
        //Deproject the 2d mouse coordinate into 3d world. Store the MousePosWorldLocation and normal (direction).
        Canvas.DeProject(PC.PlayerMouse, PC.MousePosWorldLocation, PC.MousePosWorldNormal);

        //Get a type casted reference to our custom camera.
        PlayerCam = FireTeamIsoCam(PC.PlayerCamera);

        //Calculate a trace from Player camera + 100 up(z) in direction of deprojected MousePosWorldNormal (the direction of the mouse).
        //-----------------
        //Set the ray direction as the mouseWorldnormal
        PC.RayDir = PC.MousePosWorldNormal;
        //Start the trace at the player camera (isometric) + 100 unit z and a little offset in front of the camera (direction *10)
        PC.StartTrace = (PlayerCam.ViewTarget.POV.Location + vect(0,0,100)) + PC.RayDir * 10;
        //End this ray at start + the direction multiplied by given distance (5000 unit is far enough generally)
        PC.EndTrace = PC.StartTrace + PC.RayDir * 5000;

        //Trace MouseHitWorldLocation each frame to world location (here you can get from the trace the actors that are hit by the trace, for the sake of this
        //simple tutorial, we do noting with the result, but if you would filter clicks only on terrain, or if the player clicks on an npc, you would want to inspect
        //the object hit in the StartFire function
        PC.TraceActor = Trace(PC.MouseHitWorldLocation, PC.MouseHitWorldNormal, PC.EndTrace, PC.StartTrace, true);

        //Calculate the pawn eye location for debug ray and for checking obstacles on click.
        PC.PawnEyeLocation = Pawn(PlayerOwner.ViewTarget).Location + Pawn(PlayerOwner.ViewTarget).EyeHeight * vect(0,0,1);

        //Your basic draw hud routine
		DrawHUD();
		if(bDrawTraces)
		{
			//If display is enabled from console, then draw Pathfinding routes and rays.
			super.DrawRoute(Pawn(PlayerOwner.ViewTarget));
			DrawTraceDebugRays();
		}
}

/**
 * This is the main drawing pump.  It will determine which hud we need to draw (Game or PostGame).  Any drawing that should occur
 * regardless of the game state should go here.
 */
function DrawHUD()
{
	//local Vector Direction;
	local string StringMessage;

	//Display traced actor class under mouse cursor for fun 
	if(FireTeamPlayerController(PlayerOwner).TraceActor != none)
	{
		StringMessage = "Actor selected:"@FireTeamPlayerController(PlayerOwner).TraceActor.class;
	}

	// now draw string with GoldColor color defined in defaultproperties. note you can
	// alternatively use MakeColor(R,G,B,A)
	Canvas.DrawColor = MakeColor(255,183,11,255);
	Canvas.SetPos( 250, 10 );
	Canvas.DrawText( StringMessage, false, , , TextRenderInfo );
}